package com.lekang.le.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Anonymous;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.le.domain.Contract;
import com.lekang.le.service.IContractService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 合同跟踪Controller
 * 
 * @author szh
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/le/contract")
public class ContractController extends BaseController
{
    @Autowired
    private IContractService contractService;
    /** 单个合同状态更新接口 */
    @PreAuthorize("@ss.hasPermi('le:contract:updateStatus')")
    @PostMapping("/status/update/{id}")
    public AjaxResult updateStatus(@PathVariable Long id) {
        contractService.calculateAndUpdateStatus(id);
        return AjaxResult.success("合同状态更新成功");
    }
    /** 批量更新所有合同状态接口 */
    @PreAuthorize("@ss.hasPermi('le:contract:batchUpdateStatus')")
    @PostMapping("/status/batch/update")
    public AjaxResult batchUpdateStatus() {
        contractService.batchCalculateAndUpdateStatus();
        return AjaxResult.success("所有合同状态批量更新成功");
    }

    /**
     * 查询合同跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('le:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract)
    {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出合同跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('le:contract:export')")
    @Log(title = "合同跟踪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contract contract)
    {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "合同跟踪数据");
    }

    /**
     * 获取合同跟踪详细信息
     */
    @PreAuthorize("@ss.hasPermi('le:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(contractService.selectContractById(id));
    }

    /**
     * 新增合同跟踪
     */
    @PreAuthorize("@ss.hasPermi('le:contract:add')")
    @Log(title = "合同跟踪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract)
    {
        return toAjax(contractService.insertContract(contract));
    }

    /**
     * 修改合同跟踪
     */
    @PreAuthorize("@ss.hasPermi('le:contract:edit')")
    @Log(title = "合同跟踪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contract contract)
    {

        System.out.println("获取前端的合同信息:::::::::::::::::::"+contract);
        return toAjax(contractService.updateContract(contract));
    }

    /**
     * 删除合同跟踪
     */
    @PreAuthorize("@ss.hasPermi('le:contract:remove')")
    @Log(title = "合同跟踪", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contractService.deleteContractByIds(ids));
    }

    /**
     * 查询我的合同列表（小程序端使用）
     * 通过wx_user_binding表关联查询当前用户绑定的老人的合同
     * 
     * @param wxUserId 微信用户ID
     * @return 合同列表
     */
    @Anonymous
    @GetMapping("/myContracts")
    public AjaxResult getMyContracts(@RequestParam(value = "wxUserId", required = true) Long wxUserId)
    {
        List<Contract> list = contractService.selectContractListByWxUserId(wxUserId);
        return success(list);
    }
}
